package com.edu.cdp.controller;

import com.edu.cdp.DTO.AddStaffDTO;
import com.edu.cdp.DTO.PageDTO;
import com.edu.cdp.DTO.StaffDTO;
import com.edu.cdp.DTO.StaffLoginDTO;
import com.edu.cdp.common.PageResult;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.Staff;
import com.edu.cdp.service.StaffService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@RestController
@Slf4j
@Tag(name = "员工相关接口", description = "此接口用于员工相关操作")
@RequestMapping("/staff")
public class StaffController {
  @Autowired
  StaffService staffService;

  @GetMapping("/list")
  @Operation(summary = "查询员工列表")
  public Result<List<Staff>> list() {
    List<Staff> list = staffService.list();

    return Result.success(list);
  }

  @PostMapping("/login")
  @Operation(summary = "员工登录", description = "员工通过工号登录")
  public Result<Staff> login(@RequestBody StaffLoginDTO staffDTO) {
    Staff staff = staffService.login(staffDTO.getJobNumber(), DigestUtils.md5DigestAsHex(staffDTO.getPassword().getBytes()));

    if (staff!= null) {
      return Result.success(staff);
    } else {
      return Result.error("工号或密码错误");
    }

  }

  @PutMapping("/update")
  @Operation(summary = "修改员工",
          description = "此接口允许员工更新其个人信息。"
  )
  public Result<Staff> update(@ParameterObject StaffDTO staff) {
    Staff newStaff = staffService.newUpdate(staff);
    log.info("性别："+ staff.getSex());
    if (newStaff != null) {
      boolean result = staffService.updateById(newStaff);
      if (result) {
        return Result.success(newStaff);
      } else {
        return Result.error("修改失败");
      }
    } else {
      return Result.error("员工不存在或员工工号已存在");
    }
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除员工")
  public Result<String> delete(
          @Parameter(name = "id", description = "员工id")
          Integer id) {
    boolean result = staffService.removeById(id);
    if (result) {
      return Result.success();
    } else {
      return Result.error("删除失败");
    }
  }

  @PostMapping("/add")
  @Operation(summary = "添加员工",
          description = "此接口用于添加员工，密码默认为1213456，" +
                  "工号为自动生成的13位数字"
  )
  public Result<Staff> add(@RequestBody AddStaffDTO staff) {
    Staff newStaff = staffService.newAdd(staff);

      return Result.success(newStaff);
  }

  @PostMapping(value = "/avatar")
  @Operation(summary = "更新头像",
          description = "此接口用于员工更新头像")
  public Result<Staff> updateAvatar(
                @Parameter(name = "id", description = "员工id")
                  Integer id,
                @Parameter(name = "avatar", description = "员工头像")
                MultipartFile avatar) throws Exception {
    Staff staff = staffService.updateAvatar(id, avatar);
    if (staff != null) {
      return Result.success(staff);
    } else {
     return Result.error("查无此员工");
    }
  }

  @GetMapping("/listByName")
  @Operation(summary = "根据员工姓名模糊查询")
  public Result<List<Staff>> staffByName(
          @Parameter(name = "name", description = "员工姓名")
          String name) {
    List<Staff> list = staffService.staffByName(name);

      return Result.success(list);
  }

  @GetMapping("/listBySex")
  @Operation(summary = "根据性别查询")
  public Result<List<Staff>> staffBySex(
          @Parameter(name = "sex", description = "员工性别")
          String sex) {
    List<Staff> list = staffService.getBySex(sex);

    return Result.success(list);
  }

  @GetMapping("/page")
  @Operation(summary = "员工分页查询")
  public Result<PageResult> page(@RequestBody PageDTO page) {
    PageResult pageResult = staffService.pageQuery(page);

    return Result.success(pageResult);
  }
}
